package com.common.wx;

import android.os.Build;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory;


/**
 * @author Lin 网络请求工具类
 */
//

public class HttpUtils {

    public static final String TAG = "HttpUtils";
    private static AsyncHttpClient client = new AsyncHttpClient(); // 实例化对象
    static {
        try {
            String versionCode = Build.VERSION.RELEASE; // 设备的系统版本
//            String versionCode = String.valueOf(SystemUtil.getVersionCode(CarZoneApplication.getAppContext()));
            client.addHeader("version", versionCode);// 系统版本

            client.addHeader("platform", "Android安卓");//app类型iOS苹果、Android安卓
        } catch (Exception e) {
//            LogUtils.e(TAG, "获取系统版本出错！");
            e.printStackTrace();
        }
        client.setTimeout(41000); // 设置链接超时，如果不设置，默认为10s

        //HTTPS
        SSLSocketFactory sf = SSLSocketFactory.getSocketFactory();
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); // 允许所有主机的验证

        //SSLSocketFactory sf = MySSLSocketFactory.getSocketFactory();
        //sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); // 允许所有主机的验证
        client.setSSLSocketFactory(sf);

    }
    /**
     * post提交
     * @param urlString 接口地址
     * @param params
     * @param res
     */
    public static void post(String urlString, RequestParams params, AsyncHttpResponseHandler res) {
        client.addHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
        client.post(urlString, params, res);
    }
    public static void get(String urlString, RequestParams params, AsyncHttpResponseHandler res) // url里面带参数
    {
        client.addHeader("Content-type", "text/html;charset=UTF-8");
        client.get(urlString, params, res);
    }
}
